<?php
namespace app\admin\model;

use think\Db;
use think\facade\Config;
use think\Model;

class BusDiversionConfig extends Model
{

    public static function getConfig($request)
    {
        $where = [];
        if ($request['id'] != '') {
            $where[] = ['diversion_id','=',$request['id']];

            return self::alias('c')
                        ->field('c.*,p.platfrom')
                        ->join('bus_platfrom p','p.plat_Id = c.plat_id')
                        ->where($where)
                        ->paginate($request['limit'], false, $request)
                        ->each(function ($item, $key) {
                            $item->platfrom = $item->platfrom . '/' . $item->plat_id;
                            switch ($item->onoff) {
                                case '1':
                                    $item->onoff_ = '<span class="layui-badge layui-bg-green status-qy">开启</span>';
                                    break;

                                default:
                                    $item->onoff_ = '<span class="layui-badge status-jy">禁用</span>';
                                    break;
                            }
                            switch ($item->isp_host) {
                                case '10086':
                                    $item->isp_host = '移动';
                                    break;
                                case '10010':
                                    $item->isp_host = '联通';
                                    break;
                                case '10000':
                                    $item->isp_host = '电信';
                                    break;
                                default:
                                    $item->isp_host = '未知';
                                    break;
                            }
                        })
                        ->toArray();
        }
    }

    /**
     * [veiwAndAdd 检查并添加]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function veiwAndAdd($request)
    {
        $configInfo = self::where('isp_host',$request['isp_host'])->where('diversion_id',$request['diversion_id'])->find();
        if ($configInfo) {
            return json(['code' => 422, 'msg' => '该渠道已有该运营商配置，请勿重复添加']);
        } else {
            $request['create_time'] = date('Y-m-d H:i:s');
            $request['update_time'] = date('Y-m-d H:i:s');
            try {
                if ($request['plat_index'] == '' || $request['plat_index'] == '0') {
                    $request['plat_index_onoff'] = 0;
                } else {
                    $request['plat_index_onoff'] = 1;
                }
                self::create($request);
                deleteCache();
                return json(['code' => 200, 'msg' => '添加成功']);
            } catch (\Exception $e) {
                trace('添加渠道关键字分流配置失败--' . $e->getMessage());
                return json(['code' => 422, 'msg' => '添加失败']);
            }
        }
    }

    /**
     * [veiwAndAdd 检查并更新]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function upd($request)
    {
        $id = $request['id'];
        unset($request['id']);
        $configInfo = self::where('isp_host',$request['isp_host'])->where('diversion_id',$request['diversion_id'])->where('id','neq',$id)->find();
        if ($configInfo) {
            return json(['code' => 422, 'msg' => '该渠道已有该运营商配置，请勿重复添加']);
        } else {
            $request['update_time'] = date('Y-m-d H:i:s');
            try {
                if ($request['plat_index'] == '' || $request['plat_index'] == '0') {
                    $request['plat_index_onoff'] = 0;
                } else {
                    $request['plat_index_onoff'] = 1;
                }
                self::where('id',$id)->update($request);
                deleteCache();
                return json(['code' => 200, 'msg' => '更新成功']);
            } catch (\Exception $e) {
                trace('更新渠道关键字分流配置失败--' . $e->getMessage());
                return json(['code' => 422, 'msg' => '更新失败']);
            }
        }
    }


    public static function onoff($request)
    {
        // trace('请求数据---'. json_encode($request));
        trace(session('username') . '把分流配置id:' . $request['id'] . '的开启状态改为了' . $request['status'], 'info');
        try {
            self::where(['id' => $request['id']])->update(['onoff' => $request['status']]);
            deleteCache();
            return json(['code' => 200, 'msg' => '更新成功']);
        } catch (\Exception $e) {
            trace('开启关键字分流失败--' . $e->getMessage());
            return json(['code' => 422 ,'msg' => '更新失败']);
        }
    }
}
